package com.musicstore.client.rest;

import java.util.List;

import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

public class RestTemplateFactory {

	public static RestTemplate getRestTemplate() {
		
		RestTemplate restTemplate = new RestTemplate();
		
		SimpleClientHttpRequestFactory httpRequestFactory = new SimpleClientHttpRequestFactory();
		
		restTemplate.setRequestFactory(httpRequestFactory);

		// web services support json responses
		MappingJacksonHttpMessageConverter simpleXmlHttpMessageConverter = new MappingJacksonHttpMessageConverter();
		final List<HttpMessageConverter<?>> listHttpMessageConverters = restTemplate.getMessageConverters();

		listHttpMessageConverters.add(simpleXmlHttpMessageConverter);
		restTemplate.setMessageConverters(listHttpMessageConverters);
		
		return restTemplate;
	}
}
